﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
namespace HA.ModFlow.Controls.Windows.DataOperation
{
    /// <summary>
    /// winKrigProgressBar.xaml 的交互逻辑
    /// </summary>
    public partial class winKrigProgressBar : Window
    {
        public winKrigProgressBar()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.updatePbDelegate = new UpdateProgressBarDelegate(this.prbar.SetValue);
            IntPtr hMenu = GetSystemMenu(new System.Windows.Interop. WindowInteropHelper(this).Handle, false);

            // this.Handle here for winform. 如果是winform程序使用this.Handle 替代 new WindowInteropHelper(this).Handle   

            int menuItemCount = GetMenuItemCount(hMenu);

            RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION); 
        }




        const int MF_BYPOSITION = 0x400;

        [DllImport("User32")]

        private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);

        [DllImport("User32")]

        private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

        [DllImport("User32")]

        private static extern int GetMenuItemCount(IntPtr hWnd);





        //Create a Delegate that matches the Signature of the ProgressBar's SetValue method
        private delegate void UpdateProgressBarDelegate(System.Windows.DependencyProperty dp, Object value);

        UpdateProgressBarDelegate updatePbDelegate;

        private double _min = 0;

        public double Min
        {
            get { return _min; }
            set
            {
                if (value < 0)
                {
                    value = 0;
                }
                _min = value;
                this.prbar.Minimum = value;
            }
        }

        private double _max = 1;

        public double Max
        {
            get { return _max; }
            set
            {
                if (value < 1)
                {
                    value = 1;
                }
                _max = value;
                this.prbar.Maximum = value;
            }
        }


        private double _value = 1;

        public double Value
        {
            get { return _value; }
            set
            {
                _value = value;
                this.Process();
            }
        }


        private void Process()
        {
            //Configure the ProgressBar
            //this.prbar.Minimum = 0;
            //this.prbar.Maximum = short.MaxValue;
            //this.prbar.Value = 0;

            //Stores the value of the ProgressBar
            //double value = 0;

            //Create a new instance of our ProgressBar Delegate that points
            //  to the ProgressBar's SetValue method.


            //Tight Loop:  Loop until the ProgressBar.Value reaches the max
            //do
            //{
            //value += 1;

            /*Update the Value of the ProgressBar:
              1)  Pass the "updatePbDelegate" delegate that points to the ProgressBar1.SetValue method
              2)  Set the DispatcherPriority to "Background"
              3)  Pass an Object() Array containing the property to update (ProgressBar.ValueProperty) and the new value */
            Dispatcher.Invoke(updatePbDelegate,
                System.Windows.Threading.DispatcherPriority.Background,
                new object[] { ProgressBar.ValueProperty, this._value });
            this.txtProgressBar.Text = (this._value/this.Max*100).ToString("F2") + "%";
            //}
            //while (this.prbar.Value != this.prbar.Maximum);

        }
    }
}
